//
//  ViewController.m
//  42centerTwo
//
//  Created by dragon on 15/10/20.
//  Copyright © 2015年 dragon. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.imageFiles = @[@"1.jpg",@"suolue_2.png",@"3.png"];
    self.pageTitles = @[@"1",@"2",@"3"];  //初始化
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"pageviewcontroller"]; //storyborad绑定
    self.pageViewController.dataSource = self;
    PageContentController * statrtViewController = [self viewContentViewController:0];
    
    NSMutableArray * viewControllers = [NSMutableArray arrayWithObject:statrtViewController];
    
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    self.pageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); //格式
    
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/



//上一页
-(UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController {
    
    NSInteger index = ((PageContentController*)viewController).pageIndex;
    if(index == 0 || index == NSNotFound) {
        return nil;
    }
    
    index--;
    return [self viewContentViewController:index];
    
}


//下一页
-(UIViewController *) pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController {
    NSInteger index = ((PageContentController*)viewController).pageIndex;
    if (index == NSNotFound) {
        
        return nil;
    }
    
    index ++;
    if (index == [self.pageTitles count]) {
        return nil;
    }
    return [self viewContentViewController:index ];
}

-(PageContentController*) viewContentViewController : (NSInteger) index {
    if ([self.pageTitles count] == 0 || index >= [self.pageTitles count]) {
         NSLog(@"index");
        return nil;
    }
    
    PageContentController *pageContentController = [self.storyboard instantiateViewControllerWithIdentifier:@"pagecontentcontroller"];
    pageContentController.imageFile = self.imageFiles[index];
    pageContentController.titleText = self.pageTitles[index];
    pageContentController.pageIndex = index;
    
    
    return pageContentController;
}


-(NSInteger) presentationCountForPageViewController:(UIPageViewController *)pageViewController {
    return [self.pageTitles count];
}

-(NSInteger) presentationIndexForPageViewController:(UIPageViewController *)pageViewController {
    return 0;
}







@end
